home *** CD-ROM | disk | FTP | other *** search
- var clpics = {}
- clpics.callbacks = {}
-
- clpics.init = function (){
- gBrowser.addEventListener("DOMContentLoaded", function (){
- clpics.pref("enable")
- ?clpics.run()
- :null
- }, false)
- }
- window.addEventListener("load", clpics.init, true)
-
- clpics.locale = function (name, args){
- var strings = document.getElementById("clpics-strings")
- return (args != undefined)
- ? strings.getFormattedString(name, args)
- : strings.getString(name)
- }
-
- clpics.pref = function (name, val){
- name = "extensions.clpics."+name
- return (val != undefined)
- ? clpics.rawpref(name, val)
- : clpics.rawpref(name)
- }
- clpics.rawpref = function (name, val){
- var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch)
- if (val != undefined)
- switch(prefs.getPrefType(name)){
- case prefs.PREF_STRING:
- return prefs.setCharPref(name, val)
- case prefs.PREF_INT:
- return prefs.setIntPref(name, val)
- case prefs.PREF_BOOL:
- return prefs.setBoolPref(name, val)
- }
- else
- switch(prefs.getPrefType(name)){
- case prefs.PREF_STRING:
- return prefs.getCharPref(name)
- case prefs.PREF_INT:
- return prefs.getIntPref(name)
- case prefs.PREF_BOOL:
- return prefs.getBoolPref(name)
- }
- }
-
- clpics.log = function (str){
- var console = Components.classes["@mozilla.org/consoleservice;1"]
- .getService(Components.interfaces.nsIConsoleService)
- console.logStringMessage("CLPics: "+str)
- }
- clpics.logobj = function (obj){
- var t = []
- for (var i in obj)
- try {
- t.push(i+": "+String(obj[i]).split("\n").reverse().pop())
- }
- catch(e){}
- clpics.log(t.sort().join("\n"))
- }
-
- clpics.get_new_image_dimensions = function (width, height){
- var results = {
- width: width,
- height: height
- }
- if (!clpics.pref("resize_images")) return results
-
- switch (clpics.pref("resize_method")) {
- case "image_scale":
- results.height = parseInt(height*clpics.pref("image_scale")/100.0)
- results.width = parseInt(width*clpics.pref("image_scale")/100.0)
- break
- case "image_maxwidth":
- results.width = Math.min(width, clpics.pref("image_maxwidth"))
- results.height = parseInt(height*(results.width/width))
- break
- case "image_maxheight":
- results.height = Math.min(height, clpics.pref("image_maxheight"))
- results.width = parseInt(width*(results.height/height))
- break
- }
- return results
- }
-
- clpics.unloaded_images = {}
- clpics.div_count = 0
- clpics.do_p = function (p){
- // Make sure we have content to fetch
- var link = $("a", p)[0]
- if (link == undefined) return
- // Fetch page content, and send to main handling function.
- // This allows for asynchronous handling
- $.ajax({
- type: "GET",
- url: link.href,
- async: true,
- success: function (text){
- // temp container to parse fetched HTML
- var temp = content.document.createElement("div")
- temp.innerHTML = text
- clpics.mod_p(p, temp)
- }
- })
- }
- clpics.mod_p = function (p, temp){
- var doc = content.document
-
- // If there's an h5, either 1) the posting is flagged, 2) the poster
- // knows how to use HTML and used h5s for some reason
- if (temp.getElementsByTagName("h5").length > 0) {
- var flagger = doc.createElement("small")
- flagger.appendChild(doc.createTextNode(" "+clpics.locale("flagged")))
- p.appendChild(flagger)
- return
- }
-
- if (clpics.pref("text_preview")) {
- // userbody - user-typed content. ie, "the ad"
- var userbody = $("#userbody", temp)[0]
-
- // No userbody probably means a stray link/paragraph
- if (userbody == undefined) return
-
- userbody = userbody.cloneNode(true)
- $(".blurbs, .blurbs+table", userbody).each(function(){
- userbody.removeChild(this)
- })
-
- // extract user content
- var text = userbody.textContent
- .replace(/" "{2,}/g, " ")
- .replace(/\n+/g, "\n")
- .replace(/^[\s\xA0]+|[\s\xA0]+$/g, "")
- .split("\n")
-
-
- if (text.length > 0) {
- // Text preview
- var preview = doc.createElement("div")
- preview.className = "clpics-preview"
- if (clpics.pref("restrict_preview")) {
- preview.style.maxHeight = clpics.pref("preview_height")+"px"
- preview.style.overflow = "auto"
- preview.style.paddingRight = "10px"
- }
- for (var i=0; i<text.length; i++) {
- var newcont = doc.createElement("p")
- newcont.textContent = text[i]
- preview.appendChild(newcont)
- }
- p.appendChild(preview)
-
- // initial css
- $(preview)
- .hide()
- .css("font-size", ".8em")
- .css("width", "60%")
-
- // show/hide image
- var image = doc.createElement("img")
- image.title = clpics.locale("togglepreview")
- image.src = "chrome://clpics/skin/show-preview.png"
- $(image).css("margin-right", ".5em")
- p.insertBefore(image, p.firstChild)
- $(image).click(function (){
- $(this).siblings(".clpics-preview").slideToggle('slow')
- })
- }
- }
-
- if (clpics.pref("image_preview")) {
- /* Begin Image Sanity Checks */
- var span = $("span", p)
- // If no <span>, skip
- if (span.length == 0) return
- span = span[0]
- // If span doesn't contain "pic" or "img", skip
- if (span.textContent.indexOf("pic") == -1 && span.textContent.indexOf("img") == -1) return
- /* End Checks */
-
- // Set up image container
- var cont = doc.createElement("div")
- cont.style.position = "relative"
- p.appendChild(cont)
-
- var images = $("img", temp)
- .each(function (){
- // prevent images from being loaded now
- this.id = this.src
- this.src = ""
- this.removeAttribute("src")
-
- // detach from temp node
- this.parentNode.removeChild(this)
- })
-
- // Images
- var limit = clpics.pref("image_limit")
- var ix = 0
- cont.id = "clpics-div-"+clpics.div_count++
- clpics.unloaded_images[cont.id] = 0
- images.each(function (){
- // Use an a element to parse the image URL -- very handy
- var parsed = doc.createElement("a")
- parsed.href = this.id
- if (clpics.pref("image_domain")) {
- if (
- !parsed.host.match("craigslist\.org$") &&
- !parsed.host.match("craigslist\.ca$")
- ) {
- if (!cont.imagesrc) cont.imagesrc = ""
- if (cont.imagesrc.indexOf(parsed.host) == -1) {
- cont.imagesrc += " "+parsed.host
- }
- return true
- }
- }
-
- // if we've reached our limit, stop iteration
- if (limit > 0 && ++ix > limit) return false
-
- // Move image to page
- cont.appendChild(this)
-
- this.src = this.id
-
- $(this).css({
- display: "none",
- margin: "5px",
- border: "#000 solid 1px"
- })
- this.className = "clpics-image"
- if (this.alt && this.alt != "") {
- this.id = "clpics-"+this.alt.split(" ")[0]
- this.removeAttribute("alt")
- }
- else
- this.removeAttribute("id")
-
- clpics.unloaded_images[cont.id]++
- this.addEventListener("load", clpics.callbacks.resize_image, false)
- })
- }
- }
-
- clpics.run = function (){
- // Limit to craigslist pages
- // process if we hav a document and (endswith .org OR endswith .ca)
- if (
- !content.document ||
- (!content.document.location.host.match("craigslist\.org$") &&
- !content.document.location.host.match("craigslist\.ca$"))
- ) return
- //if (!content.document.location.pathname == "/") return
-
- var doc = content.document
-
- // craigslist has kindly given us unique body classes to work with
- // hp - homepage
- // toc - listings, both search and category
- // posting - an ad
- if (doc.body.className.indexOf("toc") != -1) {
- $("blockquote p", doc).each(function (){
- // Check if enabled first
- if (!clpics.pref("enable")) return true
-
- // If p has flag class, skip
- if ($(this).hasClass("clpics-modded")) return true
-
- // Set classname
- $(this).addClass("clpics-modded")
-
- // Process, but only if the user wants us to
- if (clpics.pref("text_preview") || clpics.pref("image_preview")) {
- clpics.do_p(this)
- }
- })
- }
- }
-
- clpics.callbacks.resize_image = function (){
- $.fn.reverse = [].reverse
- var new_dimensions = clpics.get_new_image_dimensions(
- this.naturalWidth,
- this.naturalHeight
- )
- this.height = new_dimensions.height
- this.width = new_dimensions.width
- this.style.display = "inline"
-
-
- clpics.unloaded_images[this.parentNode.id]--
- if (clpics.unloaded_images[this.parentNode.id] == 0 && clpics.pref("hover_zoom")) {
- // re-position images
- var doc = content.document
-
- $(this.parentNode).each(function (){
- if (this.imagesrc &&
- this.imagesrc.replace(/^[\s\xA0]+|[\s\xA0]+$/g, "") != "") {
- var flagger = doc.createElement("small")
- flagger.appendChild(
- doc.createTextNode(
- " "+clpics.locale("imageshosted",
- [this.imagesrc.replace(/^[\s\xA0]+|[\s\xA0]+$/g, "").split(" ").join(", ")]
- )
- )
- )
- if (this.childNodes.length > 0) {
- this.insertBefore(doc.createElement("br"), this.firstChild)
- this.insertBefore(flagger, this.firstChild)
- }
- else
- this.appendChild(flagger)
- }
- this.style.height = $(this).height()
- })
-
- $("img", this.parentNode).reverse()
- .each(function (){
- $(this)
- .css("top", this.y-5)
- .css("left", this.x-5)
- .css("position", "absolute")
-
- this.proper_height = this.height
- this.proper_width = this.width
- this.proper_top = this.style.top
- })
- .mouseover(function (){
- $(this)
- .css("z-index", 2)
- .animate({opacity: 1.0}, clpics.pref("hover_zoom_timeout"), function (){
- $(this).animate({
- height: this.naturalHeight,
- width: this.naturalWidth
- })
- })
- })
- .mouseout(function (){
- $(this)
- .stop()
- .animate({
- height: this.proper_height,
- width: this.proper_width
- }, "normal", function (){$(this).css("z-index", 1)})
- })
- }
- }
-